///librairies necessaires
#include <stdio.h> /// already include in parseur_event.h

#include <stdlib.h>



#include "parseur_event.h"
#include "parseur_data.h"

fx_parseur_data_t fx_parseur_data[EVENT_MAX];



// nom des diff�rents evenements pour pouvoir les afficher !
char *event_name[EVENT_MAX] = {

	"AssassinWinner: Red Reached the Flag",		// AssassinWinner: Red Reached the Flag
	"AssassinWinner: Blue Reached the Flag",	// AssassinWinner: Blue Reached the Flag
	"Bomb was tossed by",			// Bomb was tossed by 6
	"Bomb was planted by",			// Bomb was planted by 6
	"Bomb was defused by",			// Bomb was defused by 8!
	"Bomb has been collected by",	// Bomb has been collected by 9
	"Bombholder is",				// Bombholder is 6
	"ClientBegin:",					// ClientBegin: 6
	"ClientConnect:",				// ClientConnect: 6
	"ClientDisconnect:",			// ClientDisconnect: 6
	"ClientUserinfo:",				// ClientUserinfo:(.*)$
	"ClientUserinfoChanged:",		// ClientUserinfoChanged:(.*)$
	"Exit: Timelimit hit.",			// Exit: Timelimit hit.
	"Exit: Fraglimit hit.",			// Exit: Fraglimit hit.
	"Flag:",						// Flag: 7 2: team_CTF_blueflag
	"Flag Return: BLUE",			// Flag Return: BLUE
	"Flag Return: RED",				// Flag Return: RED
	"Hit:",							// Hit: 7 6 3 8: MQCD||Nyod hit MQCD|H-Star in the Kevlar
	"InitGame:",					// InitGame:
	"InitRound:",					// InitRound:
	"Item:",						// Item: 6 team_CTF_redflag
	"Kill:",						// Kill: 6 6 10: rafiky killed rafiky by MOD_CHANGE_TEAM
	"Pop!",							// Pop!
	"red:",							// red:10  blue:6
	"say:",							// say: 8 Chipper: Salut! Hi!
	"sayteam:",						// sayteam: 11 Useless: soz
	"saytell:",						// saytell: 8 9 yumyum: wtf
	"score:",						// score: 137  ping: 50  client: 14 Wataii
	"ShutdownGame:",				// ShutdownGame:
	"SurvivorWinner: Draw",			// SurvivorWinner: Draw
	"SurvivorWinner: Blue",			// SurvivorWinner: Blue
	"SurvivorWinner: Red",			// SurvivorWinner: Red
	"tell:",						// tell: yumyum to mike: wtf
	"Warmup:",						// Warmup:
	"--------------"				// ------------------------------------------------------------
};



int main() {

	init_fx_parseur_data();

	arbre *a = NULL;

	a = malloc_arbre();

	load_all_arbre(a);

	char line[1024];
	FILE* file = NULL;
	file = fopen("games.log", "r");
	if (file == NULL){
		//die("ERREUR d'ouverture du fichier de log");
		return 2;
	}


	// setvbuf(file, NULL, _IONBF, 0);
	// fseek ( file , 0 , SEEK_END );
	int i = 0, min, sec;
	char* ptr_line;
	while( (void*)fgets(line, 1024, file) != NULL ){

		arbre tmp	= *a;
		struct val_event *val	= NULL;

		min = strtol(line , &ptr_line, 10);
		ptr_line = ptr_line + 1;
		sec = strtol(ptr_line , NULL , 10);
		ptr_line = line + 6;

		// comment gagner 4 comparaison :p
		// for (int i=0; i < 4; i++) {
			// if (ptr_line[ tmp->index ] > tmp->comp )
				// tmp = tmp->sup_data.child;
			// else
				// tmp = tmp->inf_data.child;
		// }

		// et maintenant compment gagner 4 lignes de code :D
		for (int i=0; i < 4; i++)
			tmp = ptr_line[ tmp->index ] > tmp->comp ? tmp->sup_data.child : tmp->inf_data.child;

		while (val == NULL) {
			if (ptr_line[ tmp->index ] > tmp->comp ) {
				if ( tmp->sup_is_val )
					val = &tmp->sup_data.val_event;
				else
					tmp = tmp->sup_data.child;
			}
			else {
				if ( tmp->inf_is_val )
					val = &tmp->inf_data.val_event;
				else
					tmp = tmp->inf_data.child;
			}
		}


		// if (i % 1000 == 0)
		//if ( val->event == EVENT_CLIENTBEGIN || val->event == EVENT_CLIENTCONNECT || val->event == EVENT_CLIENTDISCONNECT  )
			//printf("%s\n%s\n", event_name[ val->event ], line);

			if ( ptr_line[ val->data_index + 1 ] == ' ' ) {
				printf("%d:%d %s => %s%s\n", min, sec, event_name[ val->event ], (char*)(ptr_line + val->data_index + 1) , line);
				return 3;
			}

			printf("%d:%d",min, sec);
			(*fx_parseur_data[ val->event ])(val->event , (char*)(ptr_line + val->data_index + 1) );
//			call_parseur_data(val->event , (char*)(ptr_line + val->data_index + 1) );
			printf("%s\n",line);

		i++;
		// printf("%s\n", line);
	}
	printf("%d\n", i);
	fclose(file);
	free_arbre(a);

	return 1;
}

